#ifndef __PLATFORM_H__
#define __PLATFORM_H__
// 指定以下代码不进行优化
// #pragma GCC push_options
// #pragma GCC optimize ("O0")

typedef  unsigned long long     uint64_t ;
typedef  unsigned int           uint32_t ;
typedef  unsigned short         uint16_t ;
typedef  unsigned char          uint8_t ;
typedef  int                    int32_t ;
typedef  short                  int16_t ;
typedef  char                   int8_t ;

// #define MAIN_OSC  150000000     // 系统时钟 150M
// #define MAIN_OSC  148148169     // 系统时钟 148.148169M
// #define MAIN_OSC  100000000     // 系统时钟 100M
// #define MAIN_OSC  50000000     // 系统时钟 50M
// #define MAIN_OSC  36000000   // 系统时钟 36M
#define MAIN_OSC  12000000   // 系统时钟 12M
// #define MAIN_OSC  8000000   // 系统时钟 8M
// ------------------------------------------------------------------
#define LED *(volatile int*)0x80000000            // led port
#define TIK *(volatile uint32_t*)0x80010000   // ms滴答计时器，由FPGA逻辑提供
#define USTIK *(volatile uint32_t*)0x8000f000   // us滴答计时器，由FPGA逻辑提供

// ------------------------------------------------------------------
// SPI 端口地址
#define SPI_BASEADDR  (volatile uint32_t*)0x80020000  // BASEADDR 

// SPI 引脚分配
#define SPI0_PIN_MISO    0
#define SPI0_PIN_CLOCK   1
#define SPI0_PIN_SELECT  2
#define SPI0_PIN_MOSI    3

// ------------------------------------------------------------------
// I2C 端口地址
#define I2C_BASEADDR  (volatile uint32_t*)0x80030000  // BASEADDR 

// i2c0 引脚
#define I2C0_PIN_SCL   0
#define I2C0_PIN_SDA   1


// ------------------------------------------------------------------
// 软件串口发送
#define SOFT_TXD  *(volatile uint32_t*)0x80000400  

//步进电机 28byj-48
#define MOTOR_28BYJ48_BASEADDR  (volatile uint32_t*)0x80000500

//PWM输出地址
#define PWM_BASEADDR  (volatile uint32_t*)0x80000600

// 调试输出
#define DEBUG_OUT  *(volatile uint32_t*)0x8000FF00  


/**
 * @brief 按bit写数据
 * 
 */
void Serv_BitWrite(volatile uint32_t * baseaddr ,uint8_t _pin ,uint8_t _val) ;


/**
 * @brief 按IO读数据
 * 
 */
uint32_t Serv_BitRead(volatile uint32_t * baseaddr ,uint8_t _pin ) ;



void Serv_PinSet(volatile uint32_t * _port, uint8_t _pin );
void Serv_PinReset(volatile uint32_t * _port, uint8_t _pin );
uint32_t Serv_PinGet(volatile uint32_t * _port, uint8_t _pin );

// void Serv_DelayTik(const volatile int32_t _delay_ticks) ; // 延时 指令数
// void Serv_Delayms(const volatile uint32_t ms) ; // ms级别延时
uint32_t  Serv_Tik() ;


#define Serv_DelayTik(x) do { \
	uint32_t cnt = x;\
	while(cnt--) ;\
} while(0)

#define Serv_Delayms(ms) do { \
	volatile uint32_t b = TIK;\
	while(TIK <= ms + b) ;\
} while(0)
#endif 

// #pragma GCC pop_options  // 结束不优化的代码
